<?php
include_once ('configure.php'); 
include_once ('class.FileLog.php');

class Ftp{

	var $ftp_server = null;      // FTP Server Address (exlucde ftp://)
	var $ftp_user_name = null;     // FTP Server Username
	var $ftp_user_pass = null;      // Password
        var $login_result;
	var $conn_id;
	var $LOG=NULL;
	var $message;
	
        
        
	function Ftp($server=FTP_SERVER,$user=FTP_USER_NAME,$pass=FTP_PASSWORD)
	{
		$this->ftp_server = $server;      // FTP Server Address (exlucde ftp://)
		$this->ftp_user_name = $user;     // FTP Server Username
		$this->ftp_user_pass = $pass;      // Password
                $this->login_result = false;
		$this->LOG=new FileLog;
		$message="New ftp Class()";
                
	}
	
	function connetti_ftp($mode=false)
	{
                $ip=$this->ftp_server;
                $this->conn_id = ftp_connect($ip);
echo ($this->conn_id." --#".$this->ftp_server."#".$this->ftp_user_name."#".$this->ftp_user_pass."\n");
                //echo "-".$this->conn_id."-$path-\n";
                
		$this->login_result = ftp_login($this->conn_id, $this->ftp_user_name, $this->ftp_user_pass);
		ftp_pasv($this->conn_id, $mode);
                // Check open
                if ((!$this->conn_id) || (!$this->login_result)) {
                     //$this->LOG->writeLog("ERROR function connetti_ftp(): connect ".$this->ftp_server." user ".$this->ftp_user_name." pass ".$this->ftp_user_pass);
                     echo ($this->conn_id." ---".$this->ftp_server."-".$this->ftp_user_name."-".$this->ftp_user_pass);
                     return false;
                } 
                $this->LOG->writeLog("function connetti_ftp(): connect ".$this->ftp_server." id ".$this->conn_id);
                return true;
                
        }

	function getFile($local_file,$path_file)
	{
		    ftp_set_option($this->conn_id, FTP_TIMEOUT_SEC, 50);
		    $timeout = ftp_get_option($this->conn_id, FTP_TIMEOUT_SEC);
		    
		    // Verify Log In Status
		    if (!$this->login_result) {
			    $this->LOG->writeLog("function connetti_ftp(): failed connect ".$this->conn_id." ".$login_result);
		    } else {
			$this->LOG->writeLog("function connetti_ftp(): Connect ftp $this->ftp_server for user $this->ftp_user_name OK!!!!");
			ftp_pasv($this->conn_id, true);
			if (ftp_get($this->conn_id, $local_file, $path_file, FTP_BINARY))
			{
			    $this->LOG->writeLog("Successfully written to $local_file\n");
                            return true;
			}
			else
			{
			    $this->LOG->writeLog("There was a problem for download $local_file $path_file\n");
			}
		}
                return false;
	}
		    
        function disconnetti_ftp()
        {
                $this->LOG->writeLog("function disconnetti_ftp(): disconnect ftp $this->ftp_server");
                $this->LOG->close();
                ftp_close($this->conn_id);
                $conn=null;
        }

        function getNameFileType($parser)
        {
           
             if (ftp_chdir($this->conn_id, "."))
             {
                $ftp_rawlist=ftp_nlist($this->conn_id, ".");
                print_r($ftp_rawlist);
                foreach($ftp_rawlist as $file)
                {
                    echo $file;
                    if(preg_match('/\.'.$parser.'$/i',$file))
                    {
                        return $file;
                   }
                }
             }
        }
       function getLastFilesCreate($path,$lastDataProc)
       {
           setlocale(LC_TIME,"it-IT");
            if (ftp_chdir($this->conn_id, $path)) 
            {

                $ftp_rawlist=ftp_nlist($this->conn_id, ".");
                
               
                
                foreach($ftp_rawlist as $file)
                {
                   if(preg_match('/[0-9]{1,}-[0-9]{1,}-[0-9]{4}\.xls/',$file))
                    {
                       $diffTime=0;
                       $buff = ftp_mdtm($this->conn_id, $file);
                       if ($buff != -1) {
                           $dataSec= $buff;
                           $diffTime=($dataSec-$lastDataProc);
                            // somefile.txt was last modified on: March 26 2003 14:16:41.
                            echo "$file was last modified on : ".($dataSec-$lastDataProc)." ". date("F d Y H:i:s.", $dataSec)."\n";
                       } else {
                            echo "Couldn't get mdtime";
                       }
                       
                        $vinfo = preg_split("/[_\.]/", $file);
//                       
//                       list($m, $d, $y) = explode('-',trim( $vinfo[4]));
//                       $dataSec=mktime(0,0,0,$m,$d,$y);
                        $dataCheck=strftime('%Y-%m-%d %H:%M:%S',$dataSec);
//
////                      print $vinfo[4]." ".$dataSec." ".$dataCheck." ".$file."\n";
                       //il file su FTP e' stato modificato e la data e' maggiore
                       //rispetto a quello caricata nel db. Prima si basava solo sulla data senza ora
                       
                        if($diffTime >0 )  
                        {
                            $arr[$vinfo[4]] = preg_replace("/.\//","",$file);
                            print "Data FTP:".strftime('%Y-%m-%d %H:%M:%S',$dataSec)." Data DB: ".strftime('%Y-%m-%d %H:%M:%S',$lastDataProc)." File: ".$arr[$vinfo[4]]."\n";
                        }
                    }
                }
                
//                krsort($arr);
                
//                foreach($arr as $k => $v)
//                {
//                   print $k." ".$arr[$k]."\n";
//                } 
//                return array(key($arr),$arr[key($arr)]);
                print_r($arr);
                  return $arr;
            }
       }
}
?>
